package com.zhentao.controller;

import com.zhentao.dto.ResponseResult;
import com.zhentao.pojo.Minio;
import com.zhentao.service.MinioService;
import com.zhentao.utils.TextModerationPlusUtil;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.PutObjectArgs;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/minio")
@CrossOrigin(origins = "*")
public class MinioController {
    @Autowired
    private MinioService minioService;

    /**
     * 上传图片到Minio并保存到数据库
     */
    @PostMapping("/upload")
    public ResponseResult upload(@RequestParam("file") MultipartFile file,
                                @RequestParam("title") String title) {
        try {
            // 1. 文本审核 - 检查备注是否包含违规词汇
            ResponseResult checkResult = TextModerationPlusUtil.checkContent(title);
            if (checkResult.getCode() != 600) { // 600表示审核通过
                System.out.println("有违规词汇");
                return ResponseResult.errorResult(400, "有违规词汇，添加失败");
            }

            // 2. 创建Minio客户端
            MinioClient minioClient = MinioClient.builder()
                    .credentials("minioadmin", "minioadmin")
                    .endpoint("http://192.168.119.128:9000")
                    .build();

            // 3. 生成唯一文件名
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String fileName = UUID.randomUUID().toString() + extension;

            // 4. 上传文件到Minio
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket("testapi")
                    .object(fileName)
                    .contentType(file.getContentType())
                    .stream(file.getInputStream(), file.getSize(), -1)
                    .build();

            ObjectWriteResponse response = minioClient.putObject(putObjectArgs);
            System.out.println("Minio上传成功: " + response);

            // 5. 获取文件访问URL
            String fileUrl = minioClient.getObjectUrl("testapi", fileName);
            String cleanUrl = fileUrl.split("\\?")[0];
            System.out.println("文件访问地址: " + cleanUrl);

            // 6. 保存到数据库
            Minio minio = new Minio();
            minio.setPath(cleanUrl);
            minio.setTitle(title);

            boolean saveResult = minioService.save(minio);
            if (saveResult) {
                return ResponseResult.okResult(200, "添加成功");
            } else {
                return ResponseResult.errorResult(500, "数据库保存失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(500, "上传失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有图片列表
     */
    @GetMapping("/list")
    public ResponseResult list() {
            List<Minio> list = minioService.list();
            return ResponseResult.okResult(list);
    }


}
